/*
 * Copyright (C) 2007  Vianney le Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fsahoraires.gui.pages;

import static fsahoraires.cours.EmptyHoraire.EMPTY_HORAIRE;

import java.util.*;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;

import fsahoraires.cours.*;
import fsahoraires.cours.Horaire.Jour;

public abstract class HorairePage extends SimpleHtmlTemplatePage {

	protected Horaire horaire = EMPTY_HORAIRE;
	private static final int COLORS = 12;

	private boolean showCode = false, showAbreviation = true;

	public HorairePage(String title) {
		super(title, "horaire.png", "horaire.xhtml");
	}

	protected String loadContent() {
		StringBuilder str = new StringBuilder("<tr><th></th>");
		for (Jour jour : Jour.values()) {
			str.append("<th>").append(jour).append("</th>");
		}
		str.append("</tr>");
		int color = 0;
		Map<Cours, Integer> colors = new HashMap<Cours, Integer>();
		for (int tranche = 0; tranche < Horaire.TRANCHES; tranche++) {
			str.append("<tr><th>").append(tranche * 2 + 1).append(
					"<br/><br/><br/>").append(tranche * 2 + 2).append("</th>");
			for (Jour jour : Jour.values()) {
				List<Occupation> occupations = horaire.getOccupations(jour,
						tranche);
				str.append("<td");
				if (occupations.isEmpty())
					str.append(" class='empty'>");
				else if (Occupation.isConflict(occupations))
					str.append(" class='conflit'>");
				else
					str.append(">");
				for (Occupation o : occupations) {
					Integer c = colors.get(o.getCours());
					if (c == null) {
						c = color;
						colors.put(o.getCours(), c);
						color = (color + 1) % COLORS;
					}
					str.append("<div class='occupation cours").append(c);
					str.append("'>");
					String abreviation = o.getCours().getAbreviation();
					if (showCode || abreviation.length() == 0) {
						str.append("<span class='code'>");
						str.append(o.getCours().getCode());
						str.append("</span> ");
					}
					if (showAbreviation && abreviation.length() != 0) {
						str.append("<span class='abreviation'>");
						str.append(abreviation);
						str.append("</span> ");
					}
					str.append("<span class='infos'>");
					str.append(o.getInfos());
					str.append("</span><br/><span class='semaines'>");
					str.append(o.getSemaines().toString().replaceAll("<",
							"&lt;").replaceAll(">", "&gt;"));
					str.append("</span> <span class='local'>");
					for (int i = 0; i < o.getLocaux().length; i++)
						str.append(i == 0 ? "" : ", ").append(o.getLocaux()[i]);
					str.append("</span></div>");
				}
				str.append("</td>");
			}
			str.append("</tr>");
		}
		return str.toString();
	}

	protected Action[] getActions() {
		Action[] old = super.getActions();
		Action[] result = new Action[old.length + 1];
		System.arraycopy(old, 0, result, 1, old.length);
		result[0] = new Action("Mise à jour", new Listener() {
			public void handleEvent(Event event) {
				fetchHoraire();
			}
		});
		return result;
	}

	public void update() {
		updateHoraire();
		super.update();
	}

	protected Control createBottomLeft(Composite parent) {
		final Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new RowLayout());
		final Combo combo = new Combo(comp, SWT.BORDER | SWT.READ_ONLY);
		combo.setToolTipText("Affichage des noms");
		combo.add("Abréviation");
		combo.add("Code");
		combo.add("Code et abréviation");
		combo.select(0);
		combo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int index = combo.getSelectionIndex();
				showAbreviation = (index == 0 || index == 2);
				showCode = (index == 1 || index == 2);
				update();
			}
		});
		addCustomBottomLeft(comp);
		return comp;
	}

	/**
	 * Ajoute d'autres contrôles en bas à droite. Il suffit de créer avec parent
	 * comme parent. Le layout est RowLayout.
	 * 
	 * @param parent
	 */
	protected void addCustomBottomLeft(Composite parent) {
	}

	protected abstract void updateHoraire();

	protected abstract void fetchHoraire();

}
